package com.airconditioner.sales.activity;

import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.airconditioner.sales.R;
import com.airconditioner.sales.model.ModelDetailInfo;
import com.airconditioner.sales.model.ModelDetailInfoResult;
import com.airconditioner.sales.util.GlideCircleTransform;
import com.airconditioner.sales.util.ShareP;
import com.airconditioner.sales.util.Url;
import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;

import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

/**
 * 作者：shuizi_wade on 2017/1/20 10:45
 * 邮箱：674618016@qq.com
 */
@ContentView(R.layout.activity_product_details)
public class ProductDetailsActivity extends BaseActivity {
    @ViewInject(R.id.pro_det_iv)
    ImageView iv_pro;
    private int macId;
    private ModelDetailInfo modelDetailInfo;
    @ViewInject(R.id.pro_det_tv_name)
    TextView tv_name;
    @ViewInject(R.id.pro_det_tv_now_money)
    TextView tv_now_money;
    @ViewInject(R.id.pro_det_tv_old_money)
    TextView tv_old_money;
    @ViewInject(R.id.pro_det_tv_size)
    TextView tv_size;
    @ViewInject(R.id.pro_det_tv_content)
    TextView tv_content;

    @Override
    public void init() {
        super.init();
        title_iv_back.setImageResource(R.mipmap.titlebar_back_black);
        title_iv_back.setBackgroundColor(getResources().getColor(R.color.white_color));
        title_relate_layout.setBackgroundColor(getResources().getColor(R.color.white_color));
        title_tv_title.setText(R.string.home_title);
        ViewGroup.LayoutParams layoutParams = iv_pro.getLayoutParams();
        layoutParams.width = ShareP.getWidth(context);
        layoutParams.height = ShareP.getWidth(context) / 2;
        iv_pro.setLayoutParams(layoutParams);
        macId = getIntent().getExtras().getInt("macId", -1);
        RequestParams params = new RequestParams(Url.MODEL_DETAIL);
        params.addBodyParameter("macId", macId + "");
        httpPost("detail", params);
    }

    @Override
    public void success(String result, String type) {
        super.success(result, type);
        if (type.equals("detail")) {
            try {
                ModelDetailInfoResult modelDetailInfoResult = JSON.parseObject(result, ModelDetailInfoResult.class);
                if (modelDetailInfoResult.getResult() == 0) {
                    modelDetailInfo = modelDetailInfoResult.getResObj();
                    Glide.with(context).load(Url.baseUrl + modelDetailInfo.getMacPics().get(0))
                            .transform(new GlideCircleTransform(context)).into(iv_pro);
                    tv_content.setText(modelDetailInfo.getMacIntro());
                    tv_old_money.setText("￥" + modelDetailInfo.getMacAgio());
                    tv_old_money.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    tv_now_money.setText("￥" + modelDetailInfo.getMacAmount());
                    tv_size.setText(modelDetailInfo.getMacArea() + "㎡");
                    tv_name.setText(modelDetailInfo.getMacName());
                } else {
                    senToa(modelDetailInfoResult.getMsg());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Event(value = {R.id.title_iv_back, R.id.btn_pay})
    private void getEvent(View view) {
        switch (view.getId()) {
            case R.id.title_iv_back:
                finish();
                break;
            case R.id.btn_pay:
                jumpActvity(InputUserInfoActivity.class);
                break;
        }
    }
}
